<?php
declare (strict_types = 1);

namespace app\admin\controller;

use wpfly\Crypto;

class Index extends BaseController
{
    //排除登录校验的action
    protected $excludeCheckLogin = [
        'index',
        'test',
        'login',
        'authServer',
        'authLogin',
    ];

    public function index()
    {
        success([
            'title' => '后台管理系统',
            'version' => 'v1.0.0',
            'organization' => '子虚乌有科技有限公司',
            'info' => '天才是不会被打败的',
        ]);
    }

    public function test()
    {
        success($this->request->param());
    }

    protected function loginWithUser($userInfo, $expire = null)
    {
        if (!isset($expire)) {
            $expire = config('session.expire');
        }
        $userInfo['expire'] = $expire;
        $userInfo['login_time'] = time();
        unset($userInfo['password']);
        $token = sign_data($userInfo, '', false);
        $prefix = config('redis.default.prefix');
        $key = "{$prefix}:admin_login:{$token}";
        $redis = redis_handle();
        $redis->set($key, json_encode($userInfo));
        $redis->expire($key, $expire);
        $redis->close();
        if ($this->app->isDebug()) {
            cookie('admin_token', $token, $expire);
        }
        success([
            'token' => $token,
        ]);
    }

    public function login()
    {
        $data = $this->request->post(['account']);
        $this->validate($data, [
            'account' => 'require',
        ]);
        $crypto = new Crypto();
        $accountInfo = $crypto->privDecrypt($data['account']);
        if (empty($accountInfo)) {
            failure(101);
        }
        $accountInfo = json_decode($accountInfo, true);
        if (empty($accountInfo)) {
            failure(102);
        }
        if (!verify_timestamp($accountInfo, 300)) {
            failure(12);
        }
        $this->validate($accountInfo, [
            'admin_username' => 'require',
            'admin_password' => 'require',
            'captcha' => 'require|alphaNum|length:4',
        ]);
        if (!captcha_check($accountInfo['captcha'])) {
            failure(8);
        }
        $userInfo = $this->db->name('admin_login')->getInfo([
            ['username', '=', $accountInfo['admin_username']],
            ['deleted', '=', 0],
            ['enabled', '=', 1],
            ['adminrole_id', '<>', 0],
        ]);
        if (empty($userInfo) || empty($userInfo['password']) || !password_verify($accountInfo['admin_password'], $userInfo['password'])) {
            failure(123);
        }
        $expire = empty($accountInfo['keep']) ? null : 7 * 86400;
        $this->loginWithUser($userInfo, $expire);
    }

    public function logout()
    {
        $userInfo = $this->getUserInfo();
        $redis = redis_handle();
        $redis->del($this->getSessionKey());
        $redis->close();
        cookie('admin_token', null);
        success([
            'result' => '登出成功',
        ]);
    }

    public function clear()
    {
        cache()->clear();
        success([
            'result' => '服务器缓存清理成功',
        ]);
    }

    public function authServer()
    {
        success([
            'protocol' => empty(config('app.server_adminauthlogin.enable_ssl')) ? 'ws' : 'wss',
            'host' => site_url(null),
            'port' => config('app.server_adminauthlogin.port'),
        ]);
    }

    public function authLogin()
    {
        $data = $this->request->post(['auth_token']);
        $this->validate($data, [
            'auth_token' => 'require|xdigit|length:40',
        ]);
        $auth_token = $data['auth_token'];
        $cacheId = "cache_admin_auth_{$auth_token}";
        $cacheData = cache($cacheId);
        if (empty($cacheData)) {
            failure(120);
        }
        cache($cacheId, null);
        $userInfo = $this->db->name('admin_login')->getInfo([
            ['id', '=', $cacheData['adminlogin_id']],
            ['deleted', '=', 0],
            ['enabled', '=', 1],
            ['adminrole_id', '<>', 0],
        ]);
        if (empty($userInfo)) {
            failure(122);
        }
        $this->loginWithUser($userInfo);
    }

    public function getInfo()
    {
        $userInfo = $this->getUserInfo();
        success([
            'userInfo' =>
            [
                'username' => $userInfo['username'],
                'nickname' => $userInfo['nickname'],
                'avatar' => $userInfo['avatar'],
            ],
            'menuInfo' =>
            [
                [
                    'title' => '网站管理',
                    'icon' => 'fa fa-address-book',
                    'href' => '',
                    'target' => '_self',
                    'child' => [
                        [
                            'title' => '内容管理',
                            'icon' => 'fa fa-lemon-o',
                            'href' => '',
                            'target' => '_self',
                            'child' => [
                                [
                                    'title' => '文章管理',
                                    'icon' => 'fa fa-lemon-o',
                                    'href' => 'pages/welcome.html?2',
                                    'target' => '_self',
                                ],
                            ],
                        ],

                        [
                            'title' => '基础管理',
                            'icon' => 'fa fa-lemon-o',
                            'href' => '',
                            'target' => '_self',
                            'child' => [
                                [
                                    'title' => '网站设置',
                                    'icon' => 'fa fa-lemon-o',
                                    'href' => 'pages/setting_base.html',
                                    'target' => '_self',
                                ],
                                [
                                    'title' => '导航管理',
                                    'icon' => 'fa fa-lemon-o',
                                    'href' => 'pages/welcome.html?3',
                                    'target' => '_self',
                                ],
                                [
                                    'title' => '分类管理',
                                    'icon' => 'fa fa-lemon-o',
                                    'href' => 'pages/welcome.html?4',
                                    'target' => '_self',
                                ],
                                [
                                    'title' => '上传管理',
                                    'icon' => 'fa fa-lemon-o',
                                    'href' => 'pages/welcome.html?5',
                                    'target' => '_self',
                                ],
                            ],
                        ],

                        [
                            'title' => '用户中心',
                            'icon' => 'fa fa-lemon-o',
                            'href' => '',
                            'target' => '_self',
                            'child' => [
                                [
                                    'title' => '客户反馈',
                                    'icon' => 'fa fa-lemon-o',
                                    'href' => 'pages/welcome.html?6',
                                    'target' => '_self',
                                ],
                            ],
                        ],
                    ],
                ],
                [
                    'title' => '系统管理',
                    'icon' => 'fa fa-lemon-o',
                    'href' => '',
                    'target' => '_self',
                    'child' => [
                        [
                            'title' => '微信公众号',
                            'icon' => 'fa fa-lemon-o',
                            'href' => 'pages/setting_officialaccount.html',
                            'target' => '_self',
                        ],
                        [
                            'title' => '微信小程序',
                            'icon' => 'fa fa-lemon-o',
                            'href' => 'pages/setting_miniprogram.html',
                            'target' => '_self',
                        ],
                        [
                            'title' => '微信支付',
                            'icon' => 'fa fa-lemon-o',
                            'href' => 'pages/setting_wechatpay.html',
                            'target' => '_self',
                        ],
                        [
                            'title' => '微信粉丝',
                            'icon' => 'fa fa-lemon-o',
                            'href' => 'pages/welcome.html?10',
                            'target' => '_self',
                        ],
                        [
                            'title' => '公众号菜单',
                            'icon' => 'fa fa-lemon-o',
                            'href' => 'pages/welcome.html?9',
                            'target' => '_self',
                        ],
                        [
                            'title' => '后台菜单',
                            'icon' => 'fa fa-lemon-o',
                            'href' => 'pages/welcome.html?12',
                            'target' => '_self',
                        ],
                        [
                            'title' => '菜单权限',
                            'icon' => 'fa fa-lemon-o',
                            'href' => 'pages/welcome.html?13',
                            'target' => '_self',
                        ],
                        [
                            'title' => '后台管理员',
                            'icon' => 'fa fa-lemon-o',
                            'href' => 'pages/welcome.html?14',
                            'target' => '_self',
                        ],
                        [
                            'title' => '管理员角色',
                            'icon' => 'fa fa-lemon-o',
                            'href' => 'pages/welcome.html?15',
                            'target' => '_self',
                        ],
                        [
                            'title' => '接口管理',
                            'icon' => 'fa fa-lemon-o',
                            'href' => 'pages/welcome.html?16',
                            'target' => '_self',
                        ],
                        [
                            'title' => '接口权限',
                            'icon' => 'fa fa-lemon-o',
                            'href' => 'pages/welcome.html?17',
                            'target' => '_self',
                        ],
                        [
                            "title" => "管理员消息测试",
                            "href" => "pages/admin_message.html",
                            "icon" => "fa fa-envelope-o",
                            "target" => "_self",
                        ],
                    ],
                ],
                [

                    'title' => '接口文档',
                    'icon' => 'fa fa-lemon-o',
                    'href' => '',
                    'target' => '_self',
                    'child' => [
                        [
                            'title' => '架构',
                            'icon' => 'fa fa-lemon-o',
                            'href' => 'pages/doc_index.html?1',
                            'target' => '_self',
                        ],
                        [
                            'title' => '接口',
                            'icon' => 'fa fa-lemon-o',
                            'href' => 'pages/doc_index.html?2',
                            'target' => '_self',
                        ],
                    ],
                ],
            ],
        ]);
    }
}
